<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .clock-show {
            width: 300px;
            margin: 50px auto;
            text-align: center;
        }

        .clock-time {
            height: 40px;
            font-family: "clockIcon";
            line-height: 40px;
            font-size: 32px;
        }

        .clock-date {
            height: 30px;
            line-height: 30px;
            font-size: 18px;
        }


        @font-face {
            font-family: "clockIcon";
            src: url('../font/clockicons.woff2');
        }
    </style>
</head>

<body>
    <div class="clock-show">
        <div class="clock-time"></div>
        <div class="clock-date"></div>
    </div>
</body>
<script>
    // document.getElementsByClassName(className)  在文档中获取对应class名的元素
    // 返回值: 返回一个由获取元素形成的数组(伪数组/集合) 
    // var result = document.getElementsByClassName("clock-time");
    // console.log(result);
    // console.log(result[0]);  //对应下标取值

    // 获取元素 并将元素从数据取出
    var clockTime = document.getElementsByClassName("clock-time")[0];
    var clockDate = document.getElementsByClassName("clock-date")[0];


    showClock();
    setInterval(showClock, 1000)






    function showClock() {
        // var date = new Date("2022/6/13 1:1:1");
        var date = new Date();

        // 日期部分
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();

        clockDate.innerHTML = `${year}年${month}月${day}日`;

        // 时间部分
        var hour = date.getHours();
        // hour = hour < 10 ? "0" + hour : hour;

        var minute = date.getMinutes();
        var second = date.getSeconds();

        clockTime.innerText = `${beautify(hour)}:${beautify(minute)}:${beautify(second)}`;
    }

    function beautify(num) {
        if (isNaN(num)) {
            var err = new Error("Please pass in a number");
            throw err;
        }
        return num < 10 ? "0" + num : num;
    }








</script>

</html>